package com.turkline.CatchMePolygon;

import javax.microedition.khronos.opengles.GL10;

import com.badlogic.androidgames.framework.gl.Camera2D;
import com.badlogic.androidgames.framework.gl.SpriteBatcher;
import com.badlogic.androidgames.framework.gl.TextureRegion;
import com.badlogic.androidgames.framework.impl.GLGraphics;

public class WorldRenderer
{
	private GLGraphics glGraphics;
	private SpriteBatcher batcher;
	private World world;
	private Camera2D camera;
	private float stateTime;
	
	float levelEndAlpha = 0;
	float levelEndRectY = -200;
	float gameEndRectY = -200;
	
	public WorldRenderer(GLGraphics glGraphics , SpriteBatcher batcher , World world)
	{
		this.glGraphics = glGraphics;
		this.batcher = batcher;
		this.world = world;
		camera = new Camera2D(glGraphics, 800, 480);
	}
	public void render(float deltaTime)
	{
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		camera.setViewportAndMatrices();
		
		batcher.beginBatch(Assets.items);
		batcher.drawSprite(400, 240, 800, 480, 1, Assets.backRegion);
		presentGame(); //oyun ekrani
		if(world.state == World.STATE_READY)
		{
			presentReady(deltaTime);
		} 
		else if(world.state == 2)
		{
			presentFinish();
		} 
		
		Assets.font.drawText(batcher, world.timeString, 780-world.timeString.length()*16, 36); // sag altta saniye
		batcher.endBatch();
		
		if(world.state == World.STATE_LEVEL_END)
		{
			presentLevelEnd();
		}
		if(world.state == World.STATE_GAME_END)
		{
			presentGameEnd();
		}
	}
	
	private void presentGameEnd()
	{
		gameEndRectY += (95-gameEndRectY)/10;
		batcher.beginBatch(Assets.items);
		batcher.drawSprite(400, 240, 1500, 600, levelEndAlpha, Assets.levelEndWhiteRegion);
		batcher.endBatch();
		batcher.beginBatch(Assets.items2);
		batcher.drawSprite(400, gameEndRectY, Assets.gameEndMessage.width, Assets.gameEndMessage.height, 1, Assets.gameEndMessage);
		batcher.endBatch();
	}
	
	private void presentLevelEnd()
	{
		batcher.beginBatch(Assets.items);
		levelEndAlpha += (0.6-levelEndAlpha)/10;
		levelEndRectY += (144 - levelEndRectY)/10;
		batcher.drawSprite(400, 240, 1500, 600, levelEndAlpha, Assets.levelEndWhiteRegion);
		batcher.drawSprite(400, levelEndRectY, Assets.levelEndRegion.width, Assets.levelEndRegion.height, 1, Assets.levelEndRegion);
		batcher.endBatch();
		
			batcher.beginBatch(Assets.items);
			if((int)world.score == 100)
			{
				Assets.fontBatcherBigScore.drawText(batcher, Integer.toString((int)world.score) + "%", 340, levelEndRectY +232-144, 1);
			}
			if((int)world.score >= 10 && (int)world.score <100)
			{
				Assets.fontBatcherBigScore.drawText(batcher, Integer.toString((int)world.score) + "%", 353, levelEndRectY +232-144, 1);
			}
			if((int)world.score < 10)
			{
				Assets.fontBatcherBigScore.drawText(batcher, Integer.toString((int)world.score) + "%", 368, levelEndRectY +232-144, 1);
			}
			if(world.score >= World.BRONZE_SCORE)
			{
				batcher.drawSprite(280, levelEndRectY +100-144, Assets.playNextRegion.width, Assets.playNextRegion.height, 1, Assets.playNextRegion);
			}
			else
			{
				batcher.drawSprite(280, levelEndRectY +100-144, Assets.playAgainRegion.width, Assets.playAgainRegion.height, 1, Assets.playAgainRegion);
			}
			
			batcher.endBatch();
			batcher.beginBatch(Assets.items2);
			Assets.fontBatcherSmallScore.drawText(batcher, (int)World.BRONZE_SCORE + "%", 475, levelEndRectY +42-144, 1);
			Assets.fontBatcherSmallScore.drawText(batcher, (int)World.SILVER_SCORE + "%", 475, levelEndRectY -144 +42 + 56, 1);
			Assets.fontBatcherSmallScore.drawText(batcher, (int)World.GOLD_SCORE + "%", 475, levelEndRectY -144 +42 + 56 + 56, 1);
			if(world.score >= World.GOLD_SCORE) {
				batcher.drawSprite(400, levelEndRectY + 168 - 144, Assets.levelEndGoldMedal.width , Assets.levelEndGoldMedal.height, 1, Assets.levelEndGoldMedal);
			} else if(world.score >= World.SILVER_SCORE) {
				batcher.drawSprite(400, levelEndRectY + 168 - 144, Assets.levelEndGoldMedal.width , Assets.levelEndGoldMedal.height, 1, Assets.levelEndSilverMedal);
			} else if(world.score >= World.BRONZE_SCORE) {
				batcher.drawSprite(400, levelEndRectY + 168 - 144, Assets.levelEndGoldMedal.width , Assets.levelEndGoldMedal.height, 1, Assets.levelEndBronzeMedal);
			} 
			batcher.endBatch();
	}
	
	
	private void presentGame() {
		for(int u = 0 ; u < 32 ; u++)
		{
			for(int j = 0 ; j< 20 ; j++)
			{
				if(world.totalPath.get(u).get(j).drawMe) 
				{		
					if(world.totalPath.get(u).get(j).SW) {	// bunun icindekiler kuzeydogu kuzeybati falan. ufak cizgiler true ise koyuyo
						batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x-8, world.totalPath.get(u).get(j).coordinates.y-8, -32, 32, 1, Assets.connecterRegion);
					}
					if(world.totalPath.get(u).get(j).NW) {	
						batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x-8, world.totalPath.get(u).get(j).coordinates.y+8, 32, 32, 1, Assets.connecterRegion);
					}
					if(world.totalPath.get(u).get(j).NE) {	
						batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x+8, world.totalPath.get(u).get(j).coordinates.y+8, -32, 32, 1, Assets.connecterRegion);
					}
					if(world.totalPath.get(u).get(j).SE) {	
						batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x+8, world.totalPath.get(u).get(j).coordinates.y-8, 32, 32, 1, Assets.connecterRegion);
					}
					batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x, world.totalPath.get(u).get(j).coordinates.y, 32, 32, 1, Assets.blockRectRegion);
					batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x, world.totalPath.get(u).get(j).coordinates.y, 32, 32, 1, Assets.blockGreenRegion);	
				}
				if(Math.random() > 0.97 && world.totalPath.get(u).get(j).isImportantPath) {
					batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x, world.totalPath.get(u).get(j).coordinates.y, 32, 32, 0.3f, Assets.blockYellowRegion);
				}
			}
		}
		batcher.drawSprite(world.totalPath.get(world.paths.levelPathX[world.paths.levelPathX.length-1]).get(world.paths.levelPathY[world.paths.levelPathY.length-1]).coordinates.x, world.totalPath.get(world.paths.levelPathX[world.paths.levelPathX.length-1]).get(world.paths.levelPathY[world.paths.levelPathX.length-1]).coordinates.y, 32, 32, 1, Assets.blockRectRegion);
		batcher.drawSprite(world.totalPath.get(world.paths.levelPathX[world.paths.levelPathX.length-1]).get(world.paths.levelPathY[world.paths.levelPathY.length-1]).coordinates.x, world.totalPath.get(world.paths.levelPathX[world.paths.levelPathX.length-1]).get(world.paths.levelPathY[world.paths.levelPathX.length-1]).coordinates.y, 32, 32, 1, Assets.blockLBlueRegion);
		batcher.drawSprite(world.totalPath.get(world.paths.levelPathX[0]).get(world.paths.levelPathY[0]).coordinates.x, world.totalPath.get(world.paths.levelPathX[0]).get(world.paths.levelPathY[0]).coordinates.y, 32, 32, 1, Assets.blockRectRegion);
		batcher.drawSprite(world.totalPath.get(world.paths.levelPathX[0]).get(world.paths.levelPathY[0]).coordinates.x, world.totalPath.get(world.paths.levelPathX[0]).get(world.paths.levelPathY[0]).coordinates.y, 32, 32, 1, Assets.blockBlueRegion);
	}
	
	private void presentFinish() {
		batcher.drawSprite(128, 240, 256, 192, 1, Assets.finishScreenRegion);
	}

	private void presentReady(float deltaTime) 
	{
		world.readyTime += deltaTime;
		float alpha = 1-world.readyTime/5;
		if(alpha <= 0) {alpha = 0;}
		for(int u = 0 ; u < 32 ; u++){
			for(int j = 0 ; j< 20 ; j++){
				if(world.totalPath.get(u).get(j).isPath){
					batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x, world.totalPath.get(u).get(j).coordinates.y, 32, 32, alpha/5, Assets.blockRectRegion);
					batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x, world.totalPath.get(u).get(j).coordinates.y, 32, 32, alpha, Assets.blockYellowRegion);
				}
				if(world.totalPath.get(u).get(j).isImportantPath)
				{
					batcher.drawSprite(world.totalPath.get(u).get(j).coordinates.x, world.totalPath.get(u).get(j).coordinates.y, 32, 32, alpha/2, Assets.blockOrangeRegion);
				}
			}
		}
		for(int i = 0 ; i < world.pathsBlock.size() ; i++)
		{
			batcher.drawSprite(world.pathsBlock.get(i).coordinates.x, world.pathsBlock.get(i).coordinates.y, 32, 32, world.pathsBlock.get(i).alpha / 5, Assets.blockRectRegion);
			batcher.drawSprite(world.pathsBlock.get(i).coordinates.x, world.pathsBlock.get(i).coordinates.y, 32, 32, world.pathsBlock.get(i).alpha, Assets.blockYellowRegion);
		}
		batcher.drawSprite(world.totalPath.get(world.paths.levelPathX[0]).get(world.paths.levelPathY[0]).coordinates.x, world.totalPath.get(world.paths.levelPathX[0]).get(world.paths.levelPathY[0]).coordinates.y, 32, 32, 1, Assets.blockRectRegion);
		batcher.drawSprite(world.totalPath.get(world.paths.levelPathX[0]).get(world.paths.levelPathY[0]).coordinates.x, world.totalPath.get(world.paths.levelPathX[0]).get(world.paths.levelPathY[0]).coordinates.y, 32, 32, 1, Assets.blockBlueRegion);
		batcher.drawSprite(world.totalPath.get(world.paths.levelPathX[world.paths.levelPathX.length-1]).get(world.paths.levelPathY[world.paths.levelPathY.length-1]).coordinates.x, world.totalPath.get(world.paths.levelPathX[world.paths.levelPathX.length-1]).get(world.paths.levelPathY[world.paths.levelPathX.length-1]).coordinates.y, 32, 32, 1, Assets.blockRectRegion);
		batcher.drawSprite(world.totalPath.get(world.paths.levelPathX[world.paths.levelPathX.length-1]).get(world.paths.levelPathY[world.paths.levelPathY.length-1]).coordinates.x, world.totalPath.get(world.paths.levelPathX[world.paths.levelPathX.length-1]).get(world.paths.levelPathY[world.paths.levelPathX.length-1]).coordinates.y, 32, 32, 1, Assets.blockLBlueRegion);
		batcher.drawSprite(400, 240, 256, 64, 1, Assets.startScreenRegion);
	}
}
